<?php

class smb_validation {
    private $items;

    public static $var_val_name = 'valid';

    public function __construct($fields_object){
        $this->build_validation_array($fields_object);

        add_action( 'admin_head-post.php', array($this, 'call_js') );
        add_action( 'admin_head-post-new.php', array($this, 'call_js') );
    }

    public function build_validation_array($fields_object){
        $this->items = array();
        //buildup the array of elements that require comparison. The JS can find the elements with only their IDs.
        foreach($fields_object as $one_object){
            if ( method_exists( $one_object, 'get_fields' ) ){
                $field = $one_object->get_fields();
                if ( isset( $field['compare'] ) ) {
                    if (!array_key_exists( $field['type'], $this->items ) ) {
                        $this->items[ $field['type'] ] = array( 'type' => $field['type'],
                                                                'contents' => array(),
                                                                'required' => array(),
                                                              );
                    }
                    $this->items[$field['type']]['contents'][$field['compare']] =  $one_object->get_inputid();

                    if ( isset( $field['required'] ) ){
                        $this->items[$field['type']]['required'][$field['compare']] =  $field['required'];
                    } else {
                        $this->items[$field['type']]['required'][$field['compare']] =  false;
                    }
                }
            }
        }
    }

    public function call_js(){
        if ( !smb_basefieldbox::check_page( 'post' ) ) return;

        foreach( $this->items as $one_comparison ){
            ksort($one_comparison['contents']);
            ksort($one_comparison['required']);
            $contents = $one_comparison['contents'];
            $required = $one_comparison['required'];

            if ( count($contents) > 1 ){
                echo '<script type="text/javascript">';
                    echo 'jQuery(document).submit(function(){';
                    echo 'var ' . smb_validation::$var_val_name . '=true;';
                    foreach($required as $key=>$one_required){
                        if($one_required){ $this->validate_empty_js( $contents[$key], $key , $one_comparison['type'] ); }
                    }
                    foreach($contents as $key=>$one_content){
                        $next_key = $key+1;
                        if (isset($contents[$next_key])){
                            $this->compare_js($contents[$key],$contents[$next_key], $one_comparison['type'], '>');
                        }
                    }
                    $this->compare_js( $contents[1], $contents[2], $one_comparison['type'], '>' );
                    echo 'return ' . smb_validation::$var_val_name .';';
                    echo '});';
                echo '</script>';
            } else {
                $type = $one_comparison['type'];
                unset($this->items[$type]);
                unset($type);
            }
        }
    }

    public function validate_empty_js($field, $order, $type){
        echo 'if(!jQuery("#' . $field . '").val()){';
        echo 'alert("ERROR EMPTY ' . $type . ' ' . $order . '");';
        echo smb_validation::$var_val_name . '=' . smb_validation::$var_val_name . '&&false;};';
    }

    public function compare_js($field1, $field2, $type, $comparesign){
        echo 'if(jQuery("#'. $field1 .'").val()'. $comparesign . 'jQuery("#'. $field2 . '").val()){';
        echo 'alert("ERROR '. $type .' COMPARE");';
        echo smb_validation::$var_val_name . '=' . smb_validation::$var_val_name . '&&false;};';
    }
}
?>
